#include"compile_run.hpp"
#include"../comm/httplib.h"

using namespace ns_compile_and_run;
using namespace httplib;

void Usage(const std::string& proc)
{
    std::cout << "Usage: " << "\n\t" << proc << " port" << std::endl;
}

//编译服务可能会被多个人请求，必须保证传递上来的code形成的临时文件名具有唯一性
int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }

    Server svr;
    
    svr.Get("/hello",[](const Request& req, Response& resp){
        //基本测试
        resp.set_content("hello world, 你好世界","text/plain;charset=utf-8");
    });

    svr.Post("/compile_and_run",[](const Request& req, Response& resp){
        std::string in_json = req.body;
        std::string out_json;
        CompileAndRun::Start(in_json,&out_json);
        resp.set_content(out_json,"application/json;charset=utf-8");
    });

    //svr.set_base_dir("./wwwroot");

    svr.listen("0.0.0.0",std::atoi(argv[1]));
    return 0;



    //提供的编译服务，打包成一个网络服务
    //cpp-httplib






    //json串格式
    // in_json: {"code": "#include...", "input": "","cpu_limit":1, "mem_limit":10240}
    // out_json: {"status":"0", "reason":"","stdout":"","stderr":"",}

    //通过http从client获取一个 json string
    // std::string in_json;
    // Json::Value in_value;
    // //R"()", raw string
    // in_value["code"] = R"(#include<iostream>
    // int main()
    // {
    //     std::cout << "u can see me" << std::endl;
    //     //std::cerr << "error" << std::endl;
    //     //int * p = new int[1024 * 1024 * 30];
    //     return 0;
    // })";
    // in_value["input"] = "";
    // in_value["cpu_limit"] = 1;
    // in_value["mem_limit"] = 10240*5;

    // Json::FastWriter writer;
    // in_json = writer.write(in_value);
    // std::cout << in_json << std::endl;

    // //这个是将来给客户端返回的json串
    // std::string out_json;
    // CompileAndRun::Start(in_json, &out_json);

    // std::cout << out_json << std::endl;

    // return 0;
}

